//package com.metaverse.backend.mq;
//
//import com.metaverse.backend.config.MqConstant;
//import com.metaverse.backend.domain.PlayerData;
//import com.metaverse.backend.douyin.PlayerDataDTO;
//import com.metaverse.backend.repo.PlayerDataRepo;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.ConsumeMode;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.atomic.AtomicInteger;
//
///**
// * 用户数据持久化
// *
// */
//@AllArgsConstructor
//@Slf4j
//@RocketMQMessageListener(
//        consumerGroup= MqConstant.PLAYER_DATA_RANK,
//        topic= MqConstant.PLAYER_DATA_RANK,
//        consumeMode = ConsumeMode.ORDERLY
//)
//@Component
//public class PlayerDataListener implements RocketMQListener<String> {
//
//    private PlayerDataRepo playerDataRepo;
//
//
//    private RedisTemplate redisTemplate;
//
//    @Override
//    public void onMessage(String dataMapKey) {
//        log.info("游戏结束，同步玩家数据：{}" , dataMapKey);
//
//        AtomicInteger saveCount = new AtomicInteger();
//        List<PlayerData> playerDataList = new ArrayList<>();
//
//        List<Object> list = redisTemplate.opsForHash().values(dataMapKey);
//        list.forEach(obj -> {
//            PlayerDataDTO item = (PlayerDataDTO)obj;
//            PlayerData playerData = playerDataRepo.findBySecOpenid(item.getSec_openid()).orElse(null);
//            if(playerData == null){
//                playerData = new PlayerData();
//                playerData.setSec_openid(item.getSec_openid());
//            }
//            playerData.setAvatar_url(item.getAvatar_url());
//            playerData.setNickname(item.getNickname());
//            playerData.setPreWorldRank(Long.parseLong(item.getPreWorldRank().toString()));
//            playerData.setWorldRank(Long.parseLong(item.getWorldRank().toString()));
//            playerData.setScore(item.getScore());
//            playerData.setAllscore(item.getAllscore());
//            if(playerData.getJoinTimes() == null){
//                playerData.setJoinTimes(0L);
//            }
//            playerData.setJoinTimes(playerData.getJoinTimes() + 1);
//            if(playerData.getVictoryTimes() == null){
//                playerData.setVictoryTimes(0L);
//            }
//            if(playerData.getFailTimes() == null){
//                playerData.setFailTimes(0L);
//            }
//
//            if(item.isVictory()){
//                playerData.setVictoryTimes(playerData.getVictoryTimes() + 1);
//            }else{
//                playerData.setFailTimes(playerData.getFailTimes() + 1);
//            }
//
//            playerDataList.add(playerData);
//
//            if(playerDataList.size() >= 500){
//                saveCount.addAndGet(playerDataList.size());
//                playerDataRepo.saveAll(playerDataList);
//                playerDataList.clear();
//            }
//
//        });
////
//        if(playerDataList.size() > 0){
//            saveCount.addAndGet(playerDataList.size());
//            playerDataRepo.saveAll(playerDataList);
//            playerDataList.clear();
//        }
//
//
//        log.info("同步玩家数据结果：{}, 数量：{}" , dataMapKey, saveCount);
//    }
//
//
//}
